package com.wealth.vouchermarket.web.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wealth.commodity.entity.Comment;
import com.wealth.commodity.entity.Goods;
import com.wealth.commodity.enums.GoodsStatusEnum;
import com.wealth.commodity.service.ICategoryService;
import com.wealth.commodity.service.ICommentService;
import com.wealth.commodity.service.IGoodsService;
import com.wealth.commodity.vo.CategoryVO;
import com.wealth.merchant.entity.Merchant;
import com.wealth.merchant.enums.MarketEnum;
import com.wealth.merchant.service.IBusinessDistrictService;
import com.wealth.merchant.service.IMerchantService;
import com.wealth.merchant.vo.DistrictVO;
import com.wealth.merchant.vo.MerchantVO;
import com.wealth.order.service.IOrderService;
import com.wealth.system.web.controller.BaseController;

import iaas.enums.StatusEnum;
import iaas.utils.ResultObject;

/**
 * 惠生活 - 商户
 * 
 * @author chan
 */
@Controller
@RequestMapping(value="/vouchermarket/merchant")
public class VoucherMarketMerchantController extends BaseController{

	Logger logger = Logger.getLogger(VoucherMarketMerchantController.class);
	
	@Autowired
	private ICategoryService categoryService;
	@Autowired
	private IMerchantService merchantService;
	@Autowired
	private IBusinessDistrictService districtService;
	@Autowired
	private IGoodsService goodsService;
	@Autowired
	private ICommentService commentService;
	@Autowired
	private IOrderService orderService;
	
	/** 
	 * 商户列表页面
	 */
	@RequestMapping
	public String listForm(Model model,HttpServletRequest request,
			@RequestParam(value="categoryid",required=false) Long categoryid,
			@RequestParam(value="districtid",required=false) Long districtid) {
		try{
			if(categoryid==null){
				categoryid = -1L;
			}
			if(districtid==null){
				districtid = -1L;
			}
			//商圈
			List<DistrictVO> districtVOList = districtService.getBusinessDistrictForHuiLife(StatusEnum.NORMAL.getValue());
			model.addAttribute("districtVOList", districtVOList);
			
			//分类
			List<CategoryVO> categoryVOList = categoryService.getCategoryForHuiLife(1L, MarketEnum.LIFE.getValue(),  StatusEnum.NORMAL.getValue());
			model.addAttribute("categoryVOList", categoryVOList);
			
			model.addAttribute("categoryid", categoryid);
			model.addAttribute("districtid", districtid);
			
			return "vouchermarket/merchant/list";
		}catch(Exception e){
			return "system/500";
		}
	}
	
	/** 
	 * 商户列表
	 */
	@RequestMapping(value="/more",method = RequestMethod.POST)
	@ResponseBody
	public ResultObject list(
			@RequestParam(defaultValue="-1L") Long categoryid,
			@RequestParam(defaultValue="-1L") Long districtid,
			@RequestParam(defaultValue="0") Integer index,
			@RequestParam(defaultValue="10") Integer offset) {
		try{
			List<MerchantVO> volist = merchantService.ajaxMerchantVO(MarketEnum.LIFE.getValue(), categoryid, districtid, 
					StatusEnum.NORMAL.getValue(), index, offset);
					
			return new ResultObject("000", "商户列表加载成功", volist);
		}catch(Exception e){
			return new ResultObject("999", "商户列表加载错误", "") ;
		}
	}
	
	
	/** 
	 * 跳转商户搜索列表页面
	 */
	@RequestMapping(value="/search",method=RequestMethod.POST)
	public String searchForm(Model model,HttpServletRequest request,
			@RequestParam(value="name",required=false) String name) {
		try{
			if(name==null){
				name = "";
			}
			model.addAttribute("name", name);
			return "vouchermarket/merchant/search";
		}catch(Exception e){
			return "system/500";
		}
	}
	
	
	/** 
	 * 商户列表
	 */
	@RequestMapping(value="/search/more",method = RequestMethod.POST)
	@ResponseBody
	public ResultObject search(
			@RequestParam(defaultValue="") String name,
			@RequestParam(defaultValue="0") Integer index,
			@RequestParam(defaultValue="10") Integer offset) {
		try{
			List<MerchantVO> volist = merchantService.ajaxSearchMerchantVO(MarketEnum.LIFE.getValue(), name,StatusEnum.NORMAL.getValue(), index, offset);
					
			return new ResultObject("000", "商户列表加载成功", volist);
		}catch(Exception e){
			return new ResultObject("999", "商户列表加载错误", "");
		}
	}
	
	
	/** 
	 * 商户详情页面
	 */
	@RequestMapping(value="/{id}")
	public String info(Model model,@PathVariable Long id) {
		try{
			//商户
			Merchant merchant = merchantService.get(id);
			model.addAttribute("merchant", merchant);
			
			//商品列表
			List<Goods> goodsList = goodsService.getGoodsByMerchantIdAndStatus(id, GoodsStatusEnum.UP.getValue());
			model.addAttribute("goodsList", goodsList);
			
			//商户最近的评论  前offset条 
			List<Comment> commentList = commentService.getLatestCommentByMerchantId(id, 1);
			model.addAttribute("commentList", commentList);
			
			//商户评价平均分数
			Integer avgScore = commentService.getAvgScoreByMerchantId(id);
			model.addAttribute("avgScore",avgScore);

			//商户评价总数
			Integer commentNum = commentService.getCommentNumByMerchantId(id);
			model.addAttribute("commentNum",commentNum);
			
			//商户月消费人数
			Date start = DateTime.now().dayOfMonth().withMinimumValue().toDate();
			Date end = DateTime.now().dayOfMonth().withMaximumValue().toDate();
			Integer avgConsumeNumber = orderService.avgConsumeNumberByMerchantId(id, start, end);
			model.addAttribute("avgConsumeNumber",avgConsumeNumber);
			
			//商户平均消费
			Integer avgTotal = orderService.avgTotalByMerchantId(id);
			model.addAttribute("avgTotal",avgTotal);
			
			return "vouchermarket/merchant/info";
		}catch(Exception e){
			return "system/500";
		}
	}
	
	
	/** 
	 * 某商户的商品列表（跳转页面）
	 */
	@RequestMapping(value="/merchantGoodsShow/{id}")
	public String merchantGoodsShow(Model model,@PathVariable Long id) {
		try{
			List<Goods> goodsList=goodsService.getGoodsByMerchantIdAndStatus(id, GoodsStatusEnum.UP.getValue());
			model.addAttribute("goodsList",goodsList);
			
			return "vouchermarket/merchant/goods/list";
		}catch(Exception e){
			return "system/500";
		}
	}
	
}
